package com.shao.im.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shao.commons.aspect.annotation.GetUser;
import com.shao.commons.domain.Codes;
import com.shao.commons.exception.ServiceException;
import com.shao.commons.utils.AuthUtils;
import com.shao.im.dao.FriendsRequestDao;
import com.shao.im.entity.FriendsRelations;
import com.shao.im.entity.FriendsRequest;
import com.shao.im.entity.vo.FriendsRequestVo;
import com.shao.im.entity.vo.RequestHandlerVo;
import com.shao.im.service.FriendsRelationsService;
import com.shao.im.service.FriendsRequestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 好友申请表(FriendsRequest)表服务实现类
 *
 * @author makejava
 * @since 2023-07-28 17:00:42
 */
@Service("friendsRequestService")
public class FriendsRequestServiceImpl extends ServiceImpl<FriendsRequestDao, FriendsRequest> implements FriendsRequestService {

    @Autowired
    private FriendsRelationsService friendsRelationsService;

    @Override
    @GetUser
    public boolean save(FriendsRequest entity) {
        Integer uid = AuthUtils.get();
        entity.setUid(uid);
        // 排除自己添加自己
        if (uid.equals(entity.getTid())) {
            throw new ServiceException(Codes.NOT_REQUEST_SELF);
        }

        FriendsRequest request = this.query()
                .eq("uid", entity.getUid())
                .eq("tid", entity.getTid())
                .one();
        if (request != null) {
            // 排除已经发送了好友申请的请求
            if (request.getStatus() == 0)
                throw new ServiceException(Codes.REQUEST_EXIST);
            // 排除同意的好友申请
            if (request.getStatus() == 1)
                throw new ServiceException(Codes.IS_FRIENDS);
        }

        // 发送好友申请
        return super.save(entity);
    }

    @Override
    @GetUser
    public int queryCount() {
        Integer uid = AuthUtils.get();
        return this.query()
                .eq("tid", uid)
                .eq("status", 0)
                .count().intValue();
    }

    /**
     * 查询我的好友申请列表
     * @return
     */
    @Override
    @GetUser
    public List<FriendsRequestVo> queryFriendRequestList() {
        Integer tid = AuthUtils.get();
        return getBaseMapper().queryFriendRequestList(tid);
    }

    /**
     * 同意/拒绝好友申请请求
     * @param requestHandlerVo
     *        id     - 发起好友申请的记录id
     *        status - 1 同意
     *               - 2 拒绝
     * @return 返回
     */
    @Override
    @Transactional
    public int requestHandler(RequestHandlerVo requestHandlerVo) {
        // 修改状态
        this.update()
                .set("status", requestHandlerVo.getStatus())
                .eq("id", requestHandlerVo.getId())
                .update();

        // 拒绝好友申请
        if (requestHandlerVo.getStatus() == 2) {
            return 1;
        }

        // 同意好友申请
        FriendsRequest request = this.query()
                .eq("id", requestHandlerVo.getId())
                .one();
        Long count = friendsRelationsService.query()
                .eq("uid", request.getUid())
                .eq("fid", request.getTid())
                .count();
        if (count > 0) {
            throw new ServiceException(Codes.IS_FRIENDS);
        }

        // 建立双向好友关系
        // 发送方建立
        friendsRelationsService.save(
                new FriendsRelations()
                        .setUid(request.getUid())
                        .setFid(request.getTid())
                        .setBeizhu(request.getBeizhu())
        );
        // 接收方建立
        friendsRelationsService.save(
                new FriendsRelations()
                        .setUid(request.getTid())
                        .setFid(request.getUid())
                        .setBeizhu(requestHandlerVo.getBeizhu())
        );
        return 1;
    }
}

